package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Currency;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class CurrencyBean extends AbstractBean{
	public static final String BEAN_NAME="currencyBean";
	
	private MasterSetupService masterSetupService;
	private List<Currency> currencyList;
	private Currency currency;
	private boolean catchError;
	
	public List<Currency> getCurrencyList() {
		return currencyList;
	}
	public void setCurrencyList(List<Currency> currencyList) {
		this.currencyList = currencyList;
	}
	public Currency getCurrency() {
		return currency;
	}
	public void setCurrency(Currency currency) {
		this.currency = currency;
	}
	public void setMasterSetupService(MasterSetupService masterSetupService) {
		this.masterSetupService = masterSetupService;
	}
	
	public void init(){
		try{
			currencyList = masterSetupService.getCurrencyList();	
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	public String navAddAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		this.currency=new Currency();
		return outcome;
	}
	public String addAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void addActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.currency.setCreatedBy(getThisUser());
			this.currency.setCreatedDate(new Date());
			this.currency.setIsDeleted(CommonConstant.NOT_DELETED);
			this.masterSetupService.saveCurrency(this.currency);
			this.currencyList = this.masterSetupService.getCurrencyList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navEditAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void navEditActionEvent(ActionEvent event){
		this.catchError=false;
		UIParameter param = (UIParameter) event.getComponent().findComponent("currencyId");
		String currencyId = (String)param.getValue();
		for(Currency currency:this.currencyList){
			if(currencyId.equalsIgnoreCase(currency.getCurrencyId())){
				this.currency = currency;
				break;
			}
		}
	}
	
	public String updateAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void updateActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.currency.setModifiedBy(getThisUser());
			this.currency.setModifiedDate(new Date());
			this.masterSetupService.updateCurrency(this.currency);
			this.currencyList=this.masterSetupService.getCurrencyList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String deleteAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		if (catchError)
			outcome=null;
		return outcome;
	}
	public void deleteActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			List<Currency> deleteList = new ArrayList<Currency>();
			for (Currency currency : this.currencyList){
				if (currency.isMarkAsAction())
					deleteList.add(currency);
			}
			this.masterSetupService.deleteCurrencies(deleteList);
			this.currencyList = this.masterSetupService.getCurrencyList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String backToListAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS;
		return outcome;
	}
}
